package tests;

import common.Utils;
import org.junit.jupiter.api.Assertions;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;


public class RegisterPage extends Utils {

    private static String registerUrl = "http://8.134.99.135:58080/sign-up.html";

    public RegisterPage() {
        super(registerUrl);
    }

    /**
     * 一、检查页面是否可以正常打开
     */
    public void checkRegister() {
        //检查log是否加载成功
        driver.findElement(By.cssSelector("body > div > div > div.text-center.mb-4 > img"));
        //检查注册表单是否加载成功
        driver.findElement(By.cssSelector("#signUpForm > div"));
        //检查标题是否加载
        String actualText = driver.findElement(By.cssSelector("#signUpForm > div > h2")).getText();
        Assertions.assertEquals("用户注册", actualText);

        //检查菜单页的内容是否已加载(按照从上到下的顺序)
        driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(2) > label"));
        driver.findElement(By.cssSelector("#username"));
        driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(3) > label"));
        driver.findElement(By.cssSelector("#nickname"));
        driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(4) > label"));
        driver.findElement(By.cssSelector("#password"));
        driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(5) > label"));
        driver.findElement(By.cssSelector("#passwordRepeat"));
        driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(6) > label"));

        //检查注册按钮的内容是否正确，按钮是否可点击
        String buttonText = driver.findElement(By.cssSelector("#submit")).getText();
        Assertions.assertEquals("注册",buttonText);
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#submit")));

    }

    /**
     * 二、异常注册
     */
    public void exceptionRegister(){
        //1、缺项填写注册信息
        //1.1不勾选同意条款
        //1.1.1不填写用户名
        driver.findElement(By.cssSelector("#nickname")).sendKeys("tests");
        driver.findElement(By.cssSelector("#password")).sendKeys("tests");
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys("tests");
        driver.findElement(By.cssSelector("#submit")).click();
        String nameText = driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(2) > div")).getText();
        Assertions.assertEquals("用户名不能为空",nameText);


        //1.1.2不填写昵称
        //先把输入框清空
        driver.findElement(By.cssSelector("#nickname")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#passwordRepeat")).clear();

        driver.findElement(By.cssSelector("#username")).sendKeys("tests");
        driver.findElement(By.cssSelector("#password")).sendKeys("tests");
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys("tests");
        driver.findElement(By.cssSelector("#submit")).click();
        String nicknameText = driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(3) > div")).getText();
        Assertions.assertEquals("昵称不能为空",nicknameText);

        //1.1.3不填写密码
        //把输入框清空
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#passwordRepeat")).clear();

        driver.findElement(By.cssSelector("#username")).sendKeys("tests");
        driver.findElement(By.cssSelector("#nickname")).sendKeys("tests");
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys("tests");
        driver.findElement(By.cssSelector("#submit")).click();
        String passwordText = driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(4) > div > div")).getText();
        Assertions.assertEquals("密码不能为空",passwordText);
        String passwordRepeatText3 = driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(5) > div > div")).getText();
        Assertions.assertEquals("请检查确认密码",passwordRepeatText3);

        //1.1.4不填写确认密码
        //将输入框清空
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#nickname")).clear();
        driver.findElement(By.cssSelector("#passwordRepeat")).clear();

        driver.findElement(By.cssSelector("#username")).sendKeys("tests");
        driver.findElement(By.cssSelector("#nickname")).sendKeys("tests");
        driver.findElement(By.cssSelector("#password")).sendKeys("tests");
        driver.findElement(By.cssSelector("#submit")).click();
        String passwordRepeatText4 = driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(5) > div > div")).getText();
        Assertions.assertEquals("请检查确认密码",passwordRepeatText4);

        //1.1.5不填写所有信息
        //将输入框清空
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#nickname")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#submit")).click();
        String nameText5 = driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(2) > div")).getText();
        Assertions.assertEquals("用户名不能为空",nameText5);
        String nicknameText5 = driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(3) > div")).getText();
        Assertions.assertEquals("昵称不能为空",nicknameText5);
        String passwordText5 = driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(4) > div > div")).getText();
        Assertions.assertEquals("密码不能为空",passwordText5);
        String passwordRepeatText5 = driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(5) > div > div")).getText();
        Assertions.assertEquals("请检查确认密码",passwordRepeatText5);

        //1.2勾选同意条款
        driver.findElement(By.cssSelector("#policy")).click();

        //1.2.1不填写用户名
        driver.findElement(By.cssSelector("#nickname")).sendKeys("tests");
        driver.findElement(By.cssSelector("#password")).sendKeys("tests");
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys("tests");
        driver.findElement(By.cssSelector("#submit")).click();
        String nameText21 = driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(2) > div")).getText();
        Assertions.assertEquals("用户名不能为空",nameText21);

        //1.2.2不填写昵称
        //先把输入框清空
        driver.findElement(By.cssSelector("#nickname")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#passwordRepeat")).clear();

        driver.findElement(By.cssSelector("#username")).sendKeys("tests");
        driver.findElement(By.cssSelector("#password")).sendKeys("tests");
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys("tests");
        driver.findElement(By.cssSelector("#submit")).click();
        String nicknameText22 = driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(3) > div")).getText();
        Assertions.assertEquals("昵称不能为空",nicknameText22);

        //1.2.3不填写密码
        //把输入框清空
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#passwordRepeat")).clear();

        driver.findElement(By.cssSelector("#username")).sendKeys("tests");
        driver.findElement(By.cssSelector("#nickname")).sendKeys("tests");
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys("tests");
        driver.findElement(By.cssSelector("#submit")).click();
        String passwordText23 = driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(4) > div > div")).getText();
        Assertions.assertEquals("密码不能为空",passwordText);
        String passwordRepeatText23 = driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(5) > div > div")).getText();
        Assertions.assertEquals("请检查确认密码",passwordRepeatText23);

        //1.2.4不填写确认密码
        //将输入框清空
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#nickname")).clear();
        driver.findElement(By.cssSelector("#passwordRepeat")).clear();

        driver.findElement(By.cssSelector("#username")).sendKeys("tests");
        driver.findElement(By.cssSelector("#nickname")).sendKeys("tests");
        driver.findElement(By.cssSelector("#password")).sendKeys("tests");
        driver.findElement(By.cssSelector("#submit")).click();
        String passwordRepeatText24 = driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(5) > div > div")).getText();
        Assertions.assertEquals("请检查确认密码",passwordRepeatText24);

        //1.2.5不填写所有信息
        //将输入框清空
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#nickname")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#submit")).click();
        String nameText25 = driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(2) > div")).getText();
        Assertions.assertEquals("用户名不能为空",nameText25);
        String nicknameText25 = driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(3) > div")).getText();
        Assertions.assertEquals("昵称不能为空",nicknameText25);
        String passwordText25 = driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(4) > div > div")).getText();
        Assertions.assertEquals("密码不能为空",passwordText25);
        String passwordRepeatText25 = driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(5) > div > div")).getText();
        Assertions.assertEquals("请检查确认密码",passwordRepeatText25);

        //2、填写完整注册信息
        //2、1，未勾选同意条款
        //将勾选条款取消掉
        driver.findElement(By.cssSelector("#policy")).click();
        //密码和确认密码一致
        driver.findElement(By.cssSelector("#username")).sendKeys("tests");
        driver.findElement(By.cssSelector("#nickname")).sendKeys("tests");
        driver.findElement(By.cssSelector("#password")).sendKeys("tests");
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys("tests");
        driver.findElement(By.cssSelector("#submit")).click();
        String classValue = driver.findElement(By.cssSelector("#policy")).getAttribute("class");
        Assertions.assertEquals("form-check-input is-invalid",classValue);


        //2、1、1密码和确认密码不一致
        //清空输入框
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#nickname")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#passwordRepeat")).clear();

        driver.findElement(By.cssSelector("#username")).sendKeys("tests");
        driver.findElement(By.cssSelector("#nickname")).sendKeys("tests");
        driver.findElement(By.cssSelector("#password")).sendKeys("tests");
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys("test1");
        driver.findElement(By.cssSelector("#submit")).click();
        String passwordRepeatText211 = driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(5) > div > div")).getText();
        Assertions.assertEquals("请检查确认密码",passwordRepeatText211);
        String classValue211 = driver.findElement(By.cssSelector("#policy")).getAttribute("class");
        Assertions.assertEquals("form-check-input is-invalid",classValue211);

        //2、2，勾选同意条款，密码和确认密码不一致
        //清空输入款所有内容
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#nickname")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#passwordRepeat")).clear();
        //勾选同意条款
        driver.findElement(By.cssSelector("#policy")).click();

        driver.findElement(By.cssSelector("#username")).sendKeys("tests");
        driver.findElement(By.cssSelector("#nickname")).sendKeys("tests");
        driver.findElement(By.cssSelector("#password")).sendKeys("tests");
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys("test1");
        driver.findElement(By.cssSelector("#submit")).click();
        String passwordRepeatText22 = driver.findElement(By.cssSelector("#signUpForm > div > div:nth-child(5) > div > div")).getText();
        Assertions.assertEquals("请检查确认密码",passwordRepeatText22);
        String classValue22 = driver.findElement(By.cssSelector("#policy")).getAttribute("class");
        Assertions.assertEquals("form-check-input is-valid",classValue22);

    }


    /**
     * 三、正常注册所有信息均填test
     */
    public void registerSuc(){
        //在异常测试时已勾选同意条款
        //将所有输入框清空
        //清空输入款所有内容
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#nickname")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#passwordRepeat")).clear();

        driver.findElement(By.cssSelector("#username")).sendKeys("tests");
        driver.findElement(By.cssSelector("#nickname")).sendKeys("tests");
        driver.findElement(By.cssSelector("#password")).sendKeys("tests");
        driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys("tests");
        driver.findElement(By.cssSelector("#submit")).click();

        //注册成功
        //使用显示等待等待弹窗的出现
        wait.until(ExpectedConditions.alertIsPresent());
        //处理欢迎的弹窗
        Alert alert = driver.switchTo().alert();
        String text = alert.getText();
        Assertions.assertEquals("注册成功，欢迎登陆！",text);
        alert.accept();
    }

}
